home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2004 #2 / Amiga Plus CD - 2004 - No. 02.iso / AmiSoft / Comm / tcp / JabberwockySRC.lha / Jabberwocky / src / console.c < prev    next >
Encoding:
C/C++ Source or Header  |  2003-01-04  |  6.0 KB  |  229 lines

  1. /*  Copyright (C) 2002 Tom Parker (tom@carrott.org),
  2.                        Matthias Münch (matthias@amigaworld.de)
  3.  
  4.     This program is free software; you can redistribute it and/or modify
  5.     it under the terms of the GNU General Public License as published by
  6.     the Free Software Foundation; either version 2 of the License, or
  7.     (at your option) any later version.
  8.  
  9.     This program is distributed in the hope that it will be useful,
  10.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.     GNU General Public License for more details.
  13.  
  14.     You should have received a copy of the GNU General Public License
  15.     along with this program; if not, write to the Free Software
  16.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  17.  
  18.     In addition, as a special exception, Tom Parker and Matthias Münch give
  19.     permission to link the code of this program with a TCP stack of your 
  20.     choice, any official MUI libraries or classes and any custom MUI classes
  21.     that should be necessary for the operation of this program.  This 
  22.     exception also gives you permission to distribute linked combinations 
  23.     including this software with any of the before-mentioned libraries and 
  24.     classes.  You must obey the GNU General Public License in all respects for
  25.     all of the code used other than that provided by the before-mentioned 
  26.     libraries and classes.  As part of this exception you are obliged to 
  27.     follow the license terms of the before-mentioned libraries, this license
  28.     does not compel you to follow those terms, but if you do not then you may
  29.     not link with those libraries.  If you modify this file, you may extend
  30.     this exception to your version of the file, but you are not obligated to
  31.     do so.  If you do not wish to do so, delete this exception statement from
  32.     your version.
  33. */
  34. /*
  35. ** xml console
  36. */
  37.  
  38. #include "common.h"
  39.  
  40. #include <time.h>
  41.  
  42. #include <MUI/TextEditor_mcc.h>
  43.  
  44. static Object *c_list;
  45. static int c_watchflag = 0;
  46.  
  47. ULONG con_new(struct IClass *cl, Object *obj, struct opSet *msg);
  48. static void con_send(struct condata *data);
  49. static void log_to_file(enum logType type, char *data);
  50.  
  51.  
  52. MUI_DISPATCH(console_dispatch)
  53. {
  54.     switch(msg->MethodID)
  55.     {
  56.     case OM_NEW:
  57.         return con_new(cl,obj,(APTR)msg);
  58.  
  59.     case CON_SEND:
  60.         con_send(INST_DATA(cl,obj));
  61.         return 0;
  62.  
  63.     case CON_SAVE:
  64.         return 0;
  65.  
  66.     case CON_TOGGLEXML:
  67.     {
  68.         struct condata *data = INST_DATA(cl,obj);
  69.         u_long tmp;
  70.         GetAttr(MUIA_Selected, data->xmltog, &tmp);
  71.         c_watchflag = tmp;
  72.         return 0;
  73.     }
  74.  
  75.  
  76.     }
  77.     return DoSuperMethodA(cl,obj,msg);
  78. }
  79.  
  80.  
  81. ULONG con_new(struct IClass *cl, Object *obj, struct opSet *msg)
  82. {
  83.     struct condata *data;
  84.     Object *xmltog, *savebut, *flushbut, *list;
  85.     Object *texted, *slider, *clrbut, *sendbut;
  86.  
  87.     obj = (Object *)DoSuperNew(cl,obj,
  88.         MUIA_Window_ID, MAKE_ID('C','O','N','S'),
  89.         MUIA_Window_Title, MSG_CONSOLE_TITLE,
  90.         MUIA_HelpNode, "window-console",
  91.         WindowContents, VGroup,
  92.             Child, HGroup,
  93.                 Child, mui_toggle(MSG_CONSOLE_WATCH_XML, &xmltog),
  94.                 Child, (ULONG) savebut = SimpleButton(MSG_CONSOLE_SAVE),
  95.                 Child, (ULONG) flushbut = SimpleButton(MSG_CONSOLE_FLUSH),
  96.             End,
  97.             Child, (ULONG) list = NewObject(gui.chatarea_mcc->mcc_Class, NULL, TAG_DONE),
  98.             Child, HGroup,
  99.                 Child, HGroup,
  100.                     MUIA_Group_HorizSpacing, 0,
  101.                     MUIA_HorizWeight, 300,
  102.                     Child, (ULONG) texted = TextEditorObject,
  103.                         MUIA_CycleChain, 1,
  104.                     End,
  105.                     Child, (ULONG) slider = ScrollbarObject, End,
  106.                 End,
  107.                 Child, VGroup,
  108.                     Child, (ULONG) clrbut = SimpleButton(MSG_CONSOLE_CLEAR),
  109.                     Child, (ULONG) sendbut = SimpleButton(MSG_CONSOLE_SEND),
  110.                 End,
  111.             End,
  112.         End,
  113.         TAG_MORE, msg->ops_AttrList);
  114.  
  115.     if(!obj) return(0);
  116.  
  117.     data = INST_DATA(cl,obj);
  118.     data->list = list;
  119.     c_list = list;
  120.     data->texted = texted;
  121.     data->xmltog = xmltog;
  122.  
  123.     set(texted,MUIA_TextEditor_Slider, (ULONG) slider);
  124.  
  125.  
  126.     DoMethod(xmltog, MUIM_Notify, MUIA_Selected, MUIV_EveryTime, obj, 1, CON_TOGGLEXML);
  127.  
  128.     DoMethod(savebut, MUIM_Notify, MUIA_Pressed, FALSE, obj, 1, CON_SAVE);
  129.     DoMethod(flushbut, MUIM_Notify, MUIA_Pressed, FALSE, list, 1, CHATAREA_FLUSH);
  130.  
  131.     DoMethod(clrbut,MUIM_Notify,MUIA_Pressed,FALSE,texted,1,MUIM_TextEditor_ClearText);
  132.     DoMethod(sendbut,MUIM_Notify,MUIA_Pressed,FALSE,obj,1,CON_SEND);
  133.  
  134.     DoMethod(obj, MUIM_Notify, MUIA_Window_CloseRequest, TRUE, obj, 3, MUIM_Set, MUIA_Window_Open, FALSE);
  135.  
  136.     return((ULONG)obj);
  137. }
  138.  
  139.  
  140. static void con_send(struct condata *data)
  141. {
  142.     char *text;
  143.  
  144.     if(net.state <= NET_CONNECT)
  145.     {
  146.         con_debug("not connected yet!");
  147.         return;
  148.     }
  149.  
  150.     text = (char *)DoMethod(data->texted, MUIM_TextEditor_ExportText);
  151.     iks_send_raw(net.parser, text);
  152.     FreeVec(text);
  153. }
  154.  
  155.  
  156. #ifdef NOMORPHOS
  157. static void console_logger_gate(void);
  158.  
  159. struct EmulLibEntry console_logger =
  160. {
  161.     TRAP_LIB, 0, (void (*)(void)) console_logger_gate
  162. };
  163.  
  164. static void console_logger_gate(void)
  165. {
  166.     ULONG *lala = (ULONG *)REG_A7;
  167.     void *udata = *lala++;
  168.     char *xml = *lala++;
  169.     int dir = *lala++;
  170.     REG_A7 = lala;
  171.  
  172. #else
  173. void SAVEDS console_logger(void *udata, char *xml, int dir)
  174. {
  175. #endif
  176.     if(dir == 0 && prf.logIncoming) log_to_file(LOG_INCOMING, xml);
  177.     if(dir != 0 && prf.logOutgoing) log_to_file(LOG_OUTGOING, xml);
  178.  
  179.     if(c_watchflag)
  180.     {
  181.         char *tmp;
  182.         if(dir == 0) tmp = "Recv"; else tmp = "Send";
  183.         DoMethod(c_list, CHATAREA_ADD, dir, tmp, xml);
  184.     }
  185. }
  186.  
  187.  
  188. void con_debug(char *msg, ...)
  189. {
  190.     static char buf2[13];
  191.     char *buf;
  192.     va_list ap;
  193.     time_t t;
  194.     struct tm *ut;
  195.  
  196.     time(&t);
  197.     ut = localtime(&t);
  198.     sprintf(buf2, "%02d:%02d:%02d", ut->tm_hour, ut->tm_min, ut->tm_sec);
  199.  
  200.     va_start(ap,msg);
  201.     buf = my_vprintf(msg, ap);
  202.     va_end(ap);
  203.  
  204.     if(prf.logDebug) log_to_file(LOG_DEBUG, buf);
  205.     DoMethod(c_list, CHATAREA_ADD, 2, buf2, buf);
  206. }
  207.  
  208.  
  209. static void log_to_file(enum logType type, char *data)
  210. {
  211.     FILE *f;
  212.  
  213.     if(!prf.logfile) return;
  214.  
  215.     f = fopen(prf.logfile, "a");
  216.     if(f)
  217.     {
  218.         switch(type)
  219.         {
  220.         case LOG_INCOMING: fputs("* XML Received:\n", f); break;
  221.         case LOG_OUTGOING: fputs("* XML Sent:\n", f); break;
  222.         case LOG_DEBUG: fputs("* Debug:\n", f); break;
  223.         }
  224.         fputs(data, f);
  225.         fputs("\n\n", f);
  226.         fclose(f);
  227.     }
  228. }
  229.